﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTD2013.Entities;
using DevExpress.XtraEditors.Controls;
using DTD2013.Common;
using DevExpress.XtraGrid.Views.Base;
using DTD2013.DTDLib;

namespace DTD2013.MainApp.UI
{
    public partial class guiNormEditor : XtraForm
    {
        #region Properties

        public CongTac DinhMucMoi { get; set; }

        #endregion

        #region Variable

        private BindingList<CongTac> CongTacList;
        private DeluxeEntities DeluxeContent;
        private string Province = string.Empty;

        #endregion

        public guiNormEditor()
        {
            InitializeComponent();
            dgcCongViec.InitializeControl();
            dgcHaoPhi.InitializeControl();
            DeluxeContent = new DeluxeEntities();
            CongTacList = new BindingList<CongTac>() { new CongTac() };
            
            dgvCongViec.ValidatingEditor += new BaseContainerValidateEditorEventHandler(dgvCongViec_ValidatingEditor);
            dgvCongViec.InvalidValueException += new InvalidValueExceptionEventHandler(dgvCongViec_InvalidValueException);

            dgvHaoPhi.CellValueChanged += new CellValueChangedEventHandler(dgvHaoPhi_CellValueChanged);
        }

        public guiNormEditor(string province) : this()
        {
            Province = province;
        }

        private void dgvHaoPhi_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            if (e.Column.FieldName.Equals(DinhMucFieldName.MaVatLieu.ToString()))
            {
                string maHP = e.Value.ToString();
                TuDienVatTu hp = DeluxeContent.TuDienVatTus.Where(t => t.MaVatLieu.Equals(maHP)).First();
                DinhMuc currentHP = dgvHaoPhi.GetRow(e.RowHandle) as DinhMuc;
                PropertiesMapper.CopyPropertiesFrom(currentHP, hp);
                currentHP.LoaiHaoPhi = DataUtilities.GetWasteTypeByWasteCode(maHP);
            }
            else if (e.Column.FieldName.Equals(DinhMucFieldName.KhoiLuong.ToString()))
            {
                DinhMuc hp = dgvHaoPhi.GetRow(e.RowHandle) as DinhMuc;
                double oldThanhTien = hp.ThanhTien;
                hp.ThanhTien = hp.KhoiLuong * hp.GiaThuc;
                CongTac cv = CongTacList[0];
                if (hp.LoaiHaoPhi.Equals(WasteType.E.ToString()))
                {
                    cv.DGNC += hp.ThanhTien - oldThanhTien;
                }
                else if (hp.LoaiHaoPhi.Equals(WasteType.F.ToString()))
                {
                    cv.DGCM += hp.ThanhTien - oldThanhTien;
                }
                else
                {
                    cv.DGVL += hp.ThanhTien - oldThanhTien;
                }
            }
        }

        private void dgvCongViec_InvalidValueException(object sender, InvalidValueExceptionEventArgs e)
        {
            e.ErrorText = MessageResource.WORKCODE_EXISTED;
        }

        private void dgvCongViec_ValidatingEditor(object sender, BaseContainerValidateEditorEventArgs e)
        {
            if (dgvCongViec.FocusedColumn.FieldName.Equals(CongTacFieldName.MaHieu.ToString()))
            {
                string maHieu = e.Value.ToString().Trim();
                IQueryable<CongTac> query = DeluxeContent.CongTacs.Where(c => c.MaHieu.Equals(maHieu));
                e.Valid = query.Count() == 0;
            }
        }

        private void guiNormEditor_Load(object sender, EventArgs e)
        {
            dgcCongViec.DataSource = CongTacList;
            dgcHaoPhi.DataSource = CongTacList[0].HaoPhiCongViecs;
            rpiLkeHaoPhi.DataSource = DeluxeContent.TuDienVatTus.Select(t => new { t.MaVatLieu, t.TenVatLieu });
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            DinhMucMoi = CongTacList[0];
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
